package com.kepler.social.common.utils;

import org.springframework.util.Assert;

import java.util.Random;

public class RandomUtil {

    public static int generateRandomNumber(int digits){
        Assert.isTrue(digits > 0 && digits < 10,  "只能生成10位以下的随机数");
        int upperLimit = 0;
        int lowerLimit = 0;
        for(int i = 0; i<digits; i++){
            upperLimit += 9*Math.pow(10, i);
            lowerLimit += Math.pow(10, i);
        }
        Random random = new Random();
        int randomNumber = random.nextInt(upperLimit - lowerLimit + 1) + lowerLimit;
        return randomNumber;
    }

}
